package com.sunshine.user.controller;


import com.sunshine.user.entity.UserProfiles;
import com.sunshine.user.service.UserProService;
import com.sunshine.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/userId")
public class UserProController {

    @Resource
    private UserProService userProService;


    @PostMapping("/findUserId")
    public Result<UserProfiles> findUserId(HttpServletRequest request)
    {
        Result<UserProfiles> userProfileByToken = userProService.findUserProfileByToken(request);
        return userProfileByToken;

    }

    @PostMapping("/addUserId")
    public Result<Boolean> addUserId(@RequestBody UserProfiles userProfiles, HttpServletRequest request)
    {
        Long userId = TokenUtil.getUserIdFromRequest(request);
        userProfiles.setUserId(userId);
        Result<Boolean> addUserProfile = userProService.addUserProfile(userProfiles);
        return addUserProfile;
    }
    
    @PostMapping(value = "/addUserIdWithAvatar", consumes = "multipart/form-data")
    public Result<Boolean> addUserIdWithAvatar(@RequestParam(required = false) MultipartFile avatarFile,
                                               @RequestParam(required = false) String nickname,
                                               @RequestParam(required = false) Integer gender,
                                               @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday,
                                               @RequestParam(required = false) String signature,
                                               HttpServletRequest request) {
        try {
            Long userId = TokenUtil.getUserIdFromRequest(request);
            
            UserProfiles userProfiles = new UserProfiles();
            userProfiles.setUserId(userId);
            userProfiles.setNickname(nickname);
            userProfiles.setGender(gender);
            userProfiles.setBirthday(birthday);
            userProfiles.setSignature(signature);
            
            Result<Boolean> addUserProfile = userProService.addUserProfile(userProfiles, avatarFile);

            return addUserProfile;
        } catch (Exception e) {
            return Result.fail("添加用户资料失败: " + e.getMessage());
        }
    }


    @PostMapping("/updateUserId")
    public Result<Boolean> updateUserId(@RequestBody UserProfiles userProfiles, HttpServletRequest request)
        {
        Long userId = TokenUtil.getUserIdFromRequest(request);
        userProfiles.setUserId(userId);
        Result<Boolean> updateUserProfile = userProService.updateUserProfile(userProfiles, request);
        return updateUserProfile;
    }
}